package com.example.module_motocircle.model

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import com.example.lib_common.appContext
import com.example.lib_common.utils.JTextUtils
import com.example.lib_common.widget.nine_pics.Image
import com.example.module_motocircle.R
import com.google.gson.annotations.SerializedName


/**
 *@author Wcj
 *@description 摩圈评论
 *@date 2022/7/12 15:59
 */
open class CommentsModel(
    val beReplyId: String = "",
    val content: String = "",
    val createBy: String = "",
    val create_name: String? = "",
    val create_time: String = "",
    val firstCommentId: String = "",
    val headImg: Image? = null,
    val ifAuthor: Int = 0,
    var isLike: Int = 0,
    val oneself: Boolean=false,
    val ifSensitive: Int = 0,
    val level: Any? = null,
    var like_number: Int = 0,
    val parentContent: Any = 0,
    val parentId: String = "",
    val replyName: String? = "",

    //渠道 1-官方 2/其他-用户
    var channel: Int? = null,
    var parentChannel: Int? = null,
    var pinned: Boolean? = false,
    //额外子评论标志位
    var twoLevelSize: Int = 0,
    var topRound: Boolean = false,
    var bottomRound: Boolean = false,
    var rect: Boolean = false,
    var isFirstLevel: Boolean = false,
    var hasChild: Boolean = false,
    var isLast: Boolean = false
) {

    fun requireAvatarUrl(): String? {
        return if (isOfficial()) {
            null
        } else {
            headImg?.url
        }
    }

    fun requireCreateName(): String {
        return if (isOfficial()) {
            appContext.getString(R.string.string_cyclone_official)
        } else {
            create_name ?: "未知用户"
        }
    }

    fun requireReplay(): String {
        return if (isParentOfficial()) {
            appContext.getString(R.string.string_cyclone_official)
        } else {
            replyName ?: "未知用户"
        }
    }

    fun isOfficial() = channel == 1

    private fun isParentOfficial() = parentChannel == 1

    fun isPin(): Boolean = pinned == true


    fun formatReplayContent(content: String, replyName: String?, context: Context): CharSequence {
        val prefix = String.format("%s", replyName ?: "未知用户")
        val formatContent = String.format("回复%s：%s", prefix, content)
        val start = formatContent.indexOf(prefix)
        val end = start + prefix.length
        val spannableStringBuilder = SpannableStringBuilder(formatContent)
        spannableStringBuilder.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_919191)),
            start,
            end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return spannableStringBuilder
    }
}

//帖子评论
data class PostCommentsModel(
    val postsCommentId: String = "",
    val postsId: String = "",
    val twoLevel: MutableList<PostCommentsModel>? = null,
) : CommentsModel()

//文章评论
data class ArticleCommentsModel(
    val articleCommentId: String = "",
    val articleId: String = "",
    val twoLevel: MutableList<ArticleCommentsModel>? = null,
) : CommentsModel()


//指南评论
data class GuideCommentsModel(
    val guideCommentId: String = "",
    val guideId: String = "",
    val twoLevel: MutableList<GuideCommentsModel>? = null,
) : CommentsModel() {

}

//路线攻略评论
data class RouteCommentsModel(
    val routeGuideCommentId: String = "",
    val routeGuideId: String = "",
    val twoLevel: MutableList<RouteCommentsModel>? = null,
) : CommentsModel()

//关于我的评论 和 评论我的
open class CommentsAbtMeModel(
    val beReplyId: String = "",
    val content: String = "",
    val createBy: String? = "",
    val createName: String? = "",
    val createTime: String = "",
    val firstCommentId: String? = "0",
    val headImg: Image? = null,
    val ifAuthor: Int? = 0,
    val ifSensitive: Int? = 0,
    val likeNumber: Int = 0,
    val parentContent: String? = "",
    val parentCreateName: String? = "",
    val parentId: String? = "",
    val replyName: String? = "",
    val threeCreateBy: String? = null,
    val threeCreateName: String? = null,
    //修改为一级是否为官方
    val channel: Int? = null,
    val parentChannel: Int? = null,
    val threeChannel: Int? = null,
    var myComment: Boolean = false,

    val postsChannel: Int? = null,
    val guideChannel: Int? = null,
    val articleChannel: Int? = null,
    //模块
    var module: Int = 0
) {

    fun requireCreateName() = if (isOfficialComments()) appContext.getString(R.string.string_cyclone_official) else createName ?: "未知用户"


    //此帖子，指南，文章是否为官方发布
    fun isOfficial() = postsChannel == 1 || guideChannel == 1 || articleChannel == 1

    //此评论是否为官方发布
    fun isOfficialComments() = channel == 1

    //显示颜色 官方-显示主题色 其他-显示黑色
    fun requireNameColor(context: Context): Int {
        return if (isOfficial()) {
            ContextCompat.getColor(context, R.color.color_d8000f)
        } else {
            ContextCompat.getColor(context, R.color.color_101010)
        }
    }

    fun requireParentCreateName(): String {
        if (parentChannel == 1)
            return appContext.getString(R.string.string_cyclone_official)
        return parentCreateName ?: ""
    }

    fun requireThreeCreateName(): String {
        if (threeChannel == 1)
            return appContext.getString(R.string.string_cyclone_official)
        return threeCreateName ?: ""
    }

//    fun transParentComment(): SpannableString {
//        // parentCreateName回复threeCreateName ：parentContent
//        return if (threeCreateName.isNullOrEmpty()) {
//            val src = "${parentCreateName ?: ""}：${parentContent ?: ""}"
//            JTextUtils.highlight(src, parentCreateName ?: "", Color.parseColor("#FF919191"))
//        } else {
//            val src = "${parentCreateName ?: ""}回复${threeCreateName}：${parentContent ?: ""}"
//            JTextUtils.highlight(
//                src, arrayOf(parentCreateName ?: "", threeCreateName),
//                Color.parseColor("#FF919191")
//            )
//        }
//    }


    fun transParentComment(): SpannableString {
        // parentCreateName回复threeCreateName ：parentContent
        return if (requireThreeCreateName().isEmpty()) {
            val src = "${requireParentCreateName()}：${parentContent ?: ""}"
            JTextUtils.highlight(src, requireParentCreateName(), Color.parseColor("#FF919191"))
        } else {
            val src = "${requireParentCreateName()}回复${requireThreeCreateName()}：${parentContent ?: ""}"
            JTextUtils.highlight(
                src, arrayOf(requireParentCreateName(), requireThreeCreateName()),
                Color.parseColor("#FF919191")
            )
        }
    }

    fun requireReplayName(): String? {
        if (parentChannel == 1) {
            return appContext.getString(R.string.string_cyclone_official)
        }
        return replyName
    }

    //修改对作品的一级评论，只需要显示内容，不用显示回复xxx哈
    fun formatReplayContent(): SpannableString {

        return if (requireReplayName().isNullOrEmpty() || (firstCommentId == "0" && parentId == "0")) {
            SpannableString(content)
        } else {
            val src = "回复${requireReplayName()}：$content"
            JTextUtils.highlight(src, requireReplayName(), Color.parseColor("#FF919191"))
        }
    }

    fun replayType() = if (parentContent == null) 1 else 2
    fun Int.formatCount(): String {
        if (this >= 10000) {
            val w = this / 10000
            return "${w}w+"
        }
        return this.toString()
    }

    fun formatLikedCount() = likeNumber.formatCount()
    fun requiredAvatar() = if (isOfficialComments()) null else headImg?.url ?: ""
    fun sliceName(): String {
        if (isOfficialComments()) {
            return appContext.getString(R.string.string_cyclone_official)
        }
        if (createName.isNullOrEmpty())
            return "未知用户"
        return if (createName.length > 10 && !TextUtils.isDigitsOnly(createName))
            createName.substring(0, 8) + "..."
        else createName
    }


    open fun requiredFirstCommentId(): String? {

        return ""
    }


    open fun ifVideo(): Boolean {
        return false
    }

    open fun requiredFirstPic(): String {
        return ""
    }

    open fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()

    open fun getFirstCommentIdOnReplay(): String {
        return ""
    }

    open fun getParentIdOnReplay(): String? {
        return ""
    }


}

/**关于我的评论 - 帖子我评论和评论我的*/
data class PostCommentsAboutMeModel(
    val img: List<Image>?,
    val postsAvatar: Image?,
    val postsCommentId: String? = "0",
    val postsContent: String,
    val postsCreateBy: String,
    val postsCreateName: String,
    val postsId: String,
) : CommentsAbtMeModel() {
    override fun requiredFirstCommentId(): String? {
        if (parentId.isNullOrEmpty() || parentId == "0") {
            // 没有PID
            return postsCommentId
        }
        return firstCommentId
    }

    override fun ifVideo(): Boolean {
        val firstOrNull = img?.firstOrNull()
        return firstOrNull?.contentType?.contains("video") == true
    }


    override fun requiredFirstPic(): String {
        val firstPic = img?.firstOrNull()?.url
        if (firstPic.isNullOrEmpty()) {
            return postsAvatar?.url ?: ""
        }
        return firstPic
    }

    override fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()


    override fun getFirstCommentIdOnReplay(): String {
        if (firstCommentId.isNullOrEmpty() || firstCommentId == "0")
            return postsCommentId ?: ""
        return firstCommentId
    }

    override fun getParentIdOnReplay(): String? {
        //   if (level == 2)
        return postsCommentId
    }
}

/**关于我的评论 - 资讯我评论和评论我的*/
data class InformationCommentsAboutMeModel(
    val img: Image?,
    val informationCommentId: String? = "0",
    val informationId: String,
    val articleContent: String?,
    val articleCreateName: String?,
) : CommentsAbtMeModel() {
    override fun requiredFirstCommentId(): String? {
        if (parentId.isNullOrEmpty() || parentId == "0") {
            // 没有PID
            return informationCommentId
        }
        return firstCommentId
    }

    override fun ifVideo(): Boolean {
        return img?.contentType?.contains("video") == true
    }


    override fun requiredFirstPic(): String {
        val firstPic = img?.url
        if (firstPic.isNullOrEmpty()) {
            return ""
        }
        return firstPic
    }

    override fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()


    override fun getFirstCommentIdOnReplay(): String {
        if (firstCommentId.isNullOrEmpty() || firstCommentId == "0")
            return informationCommentId ?: ""
        return firstCommentId
    }

    override fun getParentIdOnReplay(): String? {
        //   if (level == 2)
        return informationCommentId
    }
}

/**关于我的评论 - 文章我评论和评论我的*/
data class ArticleCommentsAbtMeModel(
    val articleAvatar: Image?,
    val articleCommentId: String,
    val articleContent: String,
    val articleCreateBy: String,
    val articleCreateName: String,
    val articleId: String,
    val img: Image?,
) : CommentsAbtMeModel() {

    override fun requiredFirstCommentId(): String? {
        if (parentId.isNullOrEmpty() || parentId == "0") {
            // 没有PID
            return articleCommentId
        }
        return firstCommentId
    }


    override fun ifVideo(): Boolean {
        return img?.contentType?.contains("video") == true
    }

    override fun requiredFirstPic(): String {
        val firstPic = img?.url
        if (firstPic.isNullOrEmpty()) {
            return articleAvatar?.url ?: ""
        }
        return firstPic
    }

    override fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()

    override fun getFirstCommentIdOnReplay(): String {
        if (firstCommentId.isNullOrEmpty() || firstCommentId == "0")
            return articleCommentId ?: ""
        return firstCommentId
    }

    override fun getParentIdOnReplay(): String? {
        //   if (level == 2)
        return articleCommentId
    }
}


/**关于我的评论 - 指南我评论和评论我的*/
data class GuideCommentsAbtMeModel(
    val guideAvatar: Image?,
    val guideCommentId: String,
    val guideContent: String,
    val guideCreateBy: String,
    val guideCreateName: String,
    val guideId: String,
    val img: Image?,
) : CommentsAbtMeModel() {

    override fun requiredFirstCommentId(): String? {
        if (parentId.isNullOrEmpty() || parentId == "0") {
            // 没有PID
            return guideCommentId
        }
        return firstCommentId
    }


    override fun ifVideo(): Boolean {
        return true
    }

    override fun requiredFirstPic(): String {
        val firstPic = img?.url
        if (firstPic.isNullOrEmpty()) {
            return guideAvatar?.url ?: ""
        }
        return firstPic
    }

    override fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()

    override fun getFirstCommentIdOnReplay(): String {
        if (firstCommentId.isNullOrEmpty() || firstCommentId == "0")
            return guideCommentId ?: ""
        return firstCommentId
    }

    override fun getParentIdOnReplay(): String? {
        //   if (level == 2)
        return guideCommentId
    }
}

// 动态
data class DynamicCommentsAbtMeModel(
    val dynamicAvatar: Image?,
    val dynamicCommentId: String,
    val dynamicContent: String,
    val dynamicCreateBy: String,
    val dynamicCreateName: String,
    val dynamicId: String,
    val articleContent: String?,
    val img: Image?,
) : CommentsAbtMeModel() {

    override fun requiredFirstCommentId(): String? {
        if (parentId.isNullOrEmpty() || parentId == "0") {
            // 没有PID
            return dynamicCommentId
        }
        return firstCommentId
    }


    override fun ifVideo(): Boolean {
        return true
    }

    override fun requiredFirstPic(): String {
        val firstPic = img?.url
        if (firstPic.isNullOrEmpty()) {
            return dynamicAvatar?.url ?: ""
        }
        return firstPic
    }

    override fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()

    override fun getFirstCommentIdOnReplay(): String {
        if (firstCommentId.isNullOrEmpty() || firstCommentId == "0")
            return dynamicCommentId ?: ""
        return firstCommentId
    }

    override fun getParentIdOnReplay(): String? {
        //   if (level == 2)
        return dynamicCommentId
    }
}

data class RouteCommentsAbtMeModel(
    val routeAvatar: Image?,
    val routeCommentId: String,
    val routeContent: String,
    val routeCreateBy: String,
    val routeCreateName: String,
    val routeGuideId: String,
    val articleContent: String?,
    val img: Image?,
) : CommentsAbtMeModel() {

    override fun requiredFirstCommentId(): String? {
        if (parentId.isNullOrEmpty() || parentId == "0") {
            // 没有PID
            return routeCommentId
        }
        return firstCommentId
    }


    override fun ifVideo(): Boolean {
        return true
    }

    override fun requiredFirstPic(): String {
        val firstPic = img?.url
        if (firstPic.isNullOrEmpty()) {
            return routeAvatar?.url ?: ""
        }
        return firstPic
    }

    override fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()

    override fun getFirstCommentIdOnReplay(): String {
        if (firstCommentId.isNullOrEmpty() || firstCommentId == "0")
            return routeCommentId ?: ""
        return firstCommentId
    }

    override fun getParentIdOnReplay(): String? {
        //   if (level == 2)
        return routeCommentId
    }
}

data class TeamCommentsAbtMeModel(
    val teamAvatar: Image?,
    val teamCommentId: String,
    val teamContent: String,
    val teamCreateBy: String,
    val teamCreateName: String,
    val teamId: String,
    val articleContent: String?,
    val img: Image?,
) : CommentsAbtMeModel() {

    override fun requiredFirstCommentId(): String? {
        if (parentId.isNullOrEmpty() || parentId == "0") {
            // 没有PID
            return teamCommentId
        }
        return firstCommentId
    }


    override fun ifVideo(): Boolean {
        return true
    }

    override fun requiredFirstPic(): String {
        val firstPic = img?.url
        if (firstPic.isNullOrEmpty()) {
            return teamAvatar?.url ?: ""
        }
        return firstPic
    }

    override fun hasPicOrAvatar() = requiredFirstPic().isNotEmpty()

    override fun getFirstCommentIdOnReplay(): String {
        if (firstCommentId.isNullOrEmpty() || firstCommentId == "0")
            return teamCommentId ?: ""
        return firstCommentId
    }

    override fun getParentIdOnReplay(): String? {
        //   if (level == 2)
        return teamCommentId
    }
}


data class AfterSalePageItem(
    val aftersaleResult: String? = null,
    val aftersaleStatus: String? = null,
    val createBy: String? = null,
    val createName: String? = null,
    val createTime: String? = null,
    val delFlag: Boolean? = null,
    val planId: String? = null,
    val postsId: String? = null,
    val updateBy: String? = null,
    val updateName: String? = null,
    val updateTime: String? = null
) {
    fun requireTitle(): String {
        return when (aftersaleStatus) {
            "0" -> "未处理"
            "1" -> "处理中"
            "2" -> "已处理"
            else -> "--"
        }
    }
}
data class PostCommentsPageModel(
    val current: Int,
    val pages: Int,
    @SerializedName("records")
    val comments: MutableList<PostCommentsModel>,
    val size: Int,
    val total: Int
)
